package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BPosterInfoEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BBusinessDocumentEntity;
import io.lkt.modules.sys.service.BBusinessDocumentService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 商学院小铺文档信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-10-23 18:17:51
 */
@RestController
@RequestMapping("/sys/bbusinessdocument")
public class BBusinessDocumentController {
    @Autowired
    private BBusinessDocumentService bBusinessDocumentService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bbusinessdocument:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bBusinessDocumentService.queryPage(params);
        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BBusinessDocumentEntity> bBusinessDocumentList = Lists.newArrayList();

            for (Object temp : list) {
                BBusinessDocumentEntity bBusinessDocumentEntity = (BBusinessDocumentEntity) temp;
                //公众号名称
                if ((bBusinessDocumentEntity.getGzhCode() != null) && (!bBusinessDocumentEntity.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bBusinessDocumentEntity.getGzhCode());
                    bBusinessDocumentEntity.setGzhName(sysGzh.getGzhName());
                } else {
                    bBusinessDocumentEntity.setGzhName("全部平台");
                }

                bBusinessDocumentList.add(bBusinessDocumentEntity);
            }
            page.setList(bBusinessDocumentList);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bbusinessdocument:info")
    public R info(@PathVariable("id") Integer id){
        BBusinessDocumentEntity bBusinessDocument = bBusinessDocumentService.getById(id);
        //公众号标识
        if (bBusinessDocument.getGzhCode() == null || bBusinessDocument.getGzhCode().equals("")) {
            bBusinessDocument.setGzhCode("-1");
        }

        return R.ok().put("bBusinessDocument", bBusinessDocument);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bbusinessdocument:save")
    public R save(@RequestBody BBusinessDocumentEntity bBusinessDocument){

        if (bBusinessDocument.getGzhCode() == null || bBusinessDocument.getGzhCode().equals("") || bBusinessDocument.getGzhCode().equals("-1")) {
            bBusinessDocument.setGzhCode("");
        }

        bBusinessDocument.setCreateTime(new Date());
        bBusinessDocument.setUpdateTime(new Date());
        bBusinessDocumentService.save(bBusinessDocument);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bbusinessdocument:update")
    public R update(@RequestBody BBusinessDocumentEntity bBusinessDocument){
        ValidatorUtils.validateEntity(bBusinessDocument);

        if (bBusinessDocument.getGzhCode() == null || bBusinessDocument.getGzhCode().equals("") || bBusinessDocument.getGzhCode().equals("-1")) {
            bBusinessDocument.setGzhCode("");
        }

        bBusinessDocument.setUpdateTime(new Date());
        bBusinessDocumentService.updateById(bBusinessDocument);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bbusinessdocument:delete")
    public R delete(@RequestBody Integer[] ids){
        bBusinessDocumentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
